package com.pine.manager.pineframework;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.pine.dao.pineframework.entity.SysRoleUser;
import com.pine.dao.pineframework.mapper.SysRoleUserMapper;
import com.pine.manager.core.BaseManager;
import com.pine.manager.pineframework.bo.SysRoleUserBo;
import com.pine.manager.pineframework.convertors.SysRoleUserConvertor;
import com.pine.manager.pineframework.query.SysRoleUserQuery;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 用户角色表 管理类
 * </p>
 *
 * @author pine manage
 * @since 2024-08-12
 */
@Component
public class SysRoleUserManager extends BaseManager<SysRoleUserMapper, SysRoleUser, Long, SysRoleUserBo, SysRoleUserQuery, SysRoleUserConvertor> {

    @Resource
    private SysRoleUserConvertor sysRoleUserConvertor;

    @Override
    public LambdaQueryWrapper<SysRoleUser> encapsulateQueryWrapper(SysRoleUserQuery query) {
        LambdaQueryWrapper<SysRoleUser> queryWrapper = super.encapsulateQueryWrapper(query);
        if (query == null) {
            return queryWrapper;
        }

        // 组装查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getRoleId()), SysRoleUser::getRoleId, query.getRoleId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getUserId()), SysRoleUser::getUserId, query.getUserId());

        return queryWrapper;
    }

    /**
     * 根据用户ID删除角色用户关联
     *
     * @param userId 用户ID
     * @return 删除结果
     */
    public boolean deleteByUserId(Long userId) {
        return this.mapper.delete(new LambdaQueryWrapper<SysRoleUser>().eq(SysRoleUser::getUserId, userId)) > 0;
    }
}
